package com.frame.platform.menu;

import java.util.ArrayList;
import java.util.List;
import com.frame.pageform.*;
import com.frame.pageform.component.JsonObject;
import com.frame.pageform.upload.PageFile;
import com.frame.platform.rank.Wfmenu;
import com.frame.util.SelectItem;
import com.frame.pageform.annotation.Login;

/**
 *菜单管理 
 * @author whf
 *
 */
@Login
public class MenuPage extends PageForm{
    static{
        initForward("/platform/menu/menu.page");
    }
    private String actions;
    private String menuId;
    private String parMenuId;
    private String menuName;
    private Wfmenu bean;
    private String state;
    private List subMenuList;
    private String[] menuIds;
    private PageFile file;
    /**
     * 打开菜单选择界面时是复选还是单选
     */
    private String type;

    public String execute() throws Exception {
        // TODO Auto-generated method stub
        if("addShow".equals(getActions())){
            setParMenuId(getMenuId());
            setActions("add");
            bean=new Wfmenu();
            bean.setIsRefresh(1);
            bean.setState(1);
            return forward("add");
        }else if("add".equals(getActions())){
            MenuDao dao=new MenuDao();
            JsonObject jsonObj=new JsonObject();
            if(dao.checkUrl(getBean().getUrl(),getBean().getId())){
                if("root".equals(getParMenuId()))
                    setParMenuId("");
                String id=dao.getKeyId(getParMenuId());
                getBean().setId(id);
                dao.addMenu(getBean());
                jsonObj.put("state", "ok");
            }
            else
                jsonObj.put("state", "exist");
            return jsonObj.toString();
        }
        else if("updateShow".equals(getActions())){
            bean=new MenuDao().getMenu(getMenuId());
            setActions("update");
            return forward("add");
        }
        else if("update".equals(getActions())){
            MenuDao dao=new MenuDao();
            JsonObject jsonObj=new JsonObject();
            if(dao.checkUrl(getBean().getUrl(),getBean().getId())){
                dao.updateMenu(getBean());
                jsonObj.put("state", "update");
            }
            else
                jsonObj.put("state", "exist");
            return jsonObj.toString();
        }
        else if("del".equals(getActions())){
            Wfmenu menu=new Wfmenu();
            menu.setId(getMenuId());
            new MenuDao().delMenu(menu);
            JsonObject jsonObj=new JsonObject();
            jsonObj.put("state", "ok");
            return jsonObj.toString();
        }
        else if("sortShow".equals(getActions())){
            if("root".equals(getParMenuId()))
                setParMenuId("");
            subMenuList=new MenuDao().getSubMenu(getParMenuId());
            setActions("sort");
            return forward("sort");
        }
        else if("sort".equals(getActions())){
            if(getMenuIds()!=null)
                new MenuDao().sortMenu(getMenuIds());
            JsonObject jsonObj=new JsonObject();
            jsonObj.put("state", "ok");
            return jsonObj.toString();
        }
        return forward();
    }
    public String getMenuUrl(){
        JsonObject jsonObj=new JsonObject();
        try{
            MenuDao mdao=new MenuDao();
            Wfmenu menu=mdao.getMenu(getMenuId());
            List<SelectItem> mlist=mdao.getMenuhome(getMenuId());
            jsonObj.put("url", menu.getUrl()==null?"":menu.getUrl());
            jsonObj.put("title",menu.getName());
            jsonObj.put("id",menu.getId());
            jsonObj.put("icon",menu.getPhoto());
            jsonObj.put("isRefresh", menu.getIsRefresh()==null?"0":menu.getIsRefresh());
            jsonObj.put("opentype", menu.getOpenType()==null?"inner":menu.getOpenType());
            jsonObj.put("javascript", menu.getJavaScript()==null?"":menu.getJavaScript());
            jsonObj.put("menuHome", mlist);
        }catch(Exception e){
            e.printStackTrace();
        }
        return jsonObj.toString();
    }
    public List getOpenTypeList(){
        List list=new ArrayList();
        list.add(new SelectItem("iframe","iframe嵌入(默认)"));
        list.add(new SelectItem("inner","系统包含"));
        list.add(new SelectItem("open","open新页"));
        list.add(new SelectItem("javascript","执行javascript脚本"));
        return list;
    }
    public String getActions() {
        return actions;
    }


    public void setActions(String actions) {
        this.actions = actions;
    }


    public String getMenuId() {
        return menuId;
    }

    public void setMenuId(String menuId) {
        this.menuId = menuId;
    }

    public String getMenuName() {
        return menuName;
    }

    public void setMenuName(String menuName) {
        this.menuName = menuName;
    }

    public String getParMenuId() {
        return parMenuId;
    }

    public void setParMenuId(String parMenuId) {
        this.parMenuId = parMenuId;
    }

    public Wfmenu getBean() {
        return bean;
    }

    public void setBean(Wfmenu bean) {
        this.bean = bean;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public List getSubMenuList() {
        return subMenuList;
    }

    public void setSubMenuList(List subMenuList) {
        this.subMenuList = subMenuList;
    }

    public String[] getMenuIds() {
        return menuIds;
    }

    public void setMenuIds(String[] menuIds) {
        this.menuIds = menuIds;
    }

    public PageFile getFile() {
        return file;
    }

    public void setFile(PageFile file) {
        this.file = file;
    }

}
